import { useEffect, useRef, useState } from 'react'

interface AudioManagerProps {
  enabled?: boolean
  onLoadComplete?: () => void
}

const AudioManager: React.FC<AudioManagerProps> = ({ enabled = true, onLoadComplete }) => {
  const [isLoaded, setIsLoaded] = useState(false)
  const [isPlaying, setIsPlaying] = useState(false)
  
  // 音频引用
  const backgroundMusicRef = useRef<HTMLAudioElement | null>(null)
  const doorOpenRef = useRef<HTMLAudioElement | null>(null)
  const doorCloseRef = useRef<HTMLAudioElement | null>(null)
  const clickRef = useRef<HTMLAudioElement | null>(null)
  const hoverRef = useRef<HTMLAudioElement | null>(null)

  // 古典主义音乐 - 使用免费的古典音乐资源
  const backgroundMusicUrl = "https://www.bensound.com/bensound-music/bensound-romantic.mp3" // 古典主义风格音乐
  
  // 音效URLs - 使用免费的音效资源
  const soundUrls = {
    doorOpen: "https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-one/zapsplat_multimedia_game_sound_positive_achievement_bell_002_53874.mp3",
    doorClose: "https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-one/zapsplat_multimedia_game_sound_negative_buzz_001_53872.mp3", 
    click: "https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-one/zapsplat_multimedia_game_sound_click_001_53870.mp3",
    hover: "https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-one/zapsplat_multimedia_game_sound_hover_001_53871.mp3"
  }

  // 初始化音频
  useEffect(() => {
    if (!enabled) return

    const loadAudio = async () => {
      try {
        // 创建背景音乐
        backgroundMusicRef.current = new Audio(backgroundMusicUrl)
        backgroundMusicRef.current.loop = true
        backgroundMusicRef.current.volume = 0.3
        backgroundMusicRef.current.preload = 'auto'

        // 创建音效
        doorOpenRef.current = new Audio(soundUrls.doorOpen)
        doorOpenRef.current.volume = 0.5
        doorOpenRef.current.preload = 'auto'

        doorCloseRef.current = new Audio(soundUrls.doorClose)
        doorCloseRef.current.volume = 0.5
        doorCloseRef.current.preload = 'auto'

        clickRef.current = new Audio(soundUrls.click)
        clickRef.current.volume = 0.4
        clickRef.current.preload = 'auto'

        hoverRef.current = new Audio(soundUrls.hover)
        hoverRef.current.volume = 0.3
        hoverRef.current.preload = 'auto'

        setIsLoaded(true)
        onLoadComplete?.()
      } catch (error) {
        console.warn('音频加载失败:', error)
      }
    }

    loadAudio()

    return () => {
      // 清理音频资源
      backgroundMusicRef.current?.pause()
      backgroundMusicRef.current = null
      doorOpenRef.current = null
      doorCloseRef.current = null
      clickRef.current = null
      hoverRef.current = null
    }
  }, [enabled, onLoadComplete])

  // 播放背景音乐
  const playBackgroundMusic = async () => {
    if (!backgroundMusicRef.current || !enabled) return
    
    try {
      await backgroundMusicRef.current.play()
      setIsPlaying(true)
    } catch (error) {
      console.warn('背景音乐播放失败:', error)
    }
  }

  // 停止背景音乐
  const stopBackgroundMusic = () => {
    if (backgroundMusicRef.current) {
      backgroundMusicRef.current.pause()
      backgroundMusicRef.current.currentTime = 0
      setIsPlaying(false)
    }
  }

  // 播放音效
  const playSound = (soundType: 'doorOpen' | 'doorClose' | 'click' | 'hover') => {
    if (!enabled) return

    const soundRef = {
      doorOpen: doorOpenRef.current,
      doorClose: doorCloseRef.current,
      click: clickRef.current,
      hover: hoverRef.current
    }[soundType]

    if (soundRef) {
      soundRef.currentTime = 0
      soundRef.play().catch(error => {
        console.warn(`${soundType} 音效播放失败:`, error)
      })
    }
  }

  // 设置音量
  const setVolume = (volume: number) => {
    if (backgroundMusicRef.current) {
      backgroundMusicRef.current.volume = Math.max(0, Math.min(1, volume))
    }
  }

  // 暴露给父组件的方法
  useEffect(() => {
    if (typeof window !== 'undefined') {
      (window as any).audioManager = {
        playBackgroundMusic,
        stopBackgroundMusic,
        playSound,
        setVolume,
        isLoaded,
        isPlaying
      }
    }
  }, [isLoaded, isPlaying])

  return null // 这个组件不渲染任何内容
}

export default AudioManager
